﻿
using System;
using System.Globalization;
using System.Linq;

struct Time
{
    public Time(int hour, int minute, int second)
        : this()
    {
        Hours = hour;
        Minutes = minute;
        Seconds = second;
    }
    public Time(string time)
        : this()
    {
        ConvertStringToValue(time);
    }

    private void ConvertStringToValue(string time)
    {
        time = time ?? "";

        int tempInt;

        Hours = time.Split(':').Length > 0 && Int32.TryParse(time.Split(':')[0], out tempInt) ? tempInt : 0;
        Minutes = time.Split(':').Length > 1 && Int32.TryParse(time.Split(':')[1], out tempInt) ? tempInt : 0;
        Seconds = time.Split(':').Length > 2 && Int32.TryParse(time.Split(':')[2], out tempInt) ? tempInt : 0;
    }
    public string Value
    {
        get
        {
            return ToString();
        }
        set
        {
            ConvertStringToValue(value);
        }
    }
    public int Hours { get; set; }
    public int Minutes { get; set; }
    public int Seconds { get; set; }
    public override string ToString()
    {
        return String.Format(
            "{0:00}:{1:00}:{2:00}",
            this.Hours, this.Minutes, this.Seconds);
    }
    public string ToString2()
    {
        return String.Format(
            "{0:00}:{1:00}",
            this.Hours, this.Minutes);
    }
}

public static class IntExtensions
{
    //public static int BgYearToDbYear(this int year, int month)
    //{ 
    //    if (month < 10)
    //    {
    //        year -= 1;
    //    }
    //    return year;
    //} 
}

public static class DateTimeExtensions
{
    public static string ToDbFormatDateTime(this DateTime? dt)
    {
        return !dt.HasValue ? null : dt.Value.ToString("yyyy-MM-dd HH:mm:ss", new CultureInfo("en-US"));
    }

    public static string ToDbFormat(this DateTime dateTime)
    {
        return dateTime.ToString("yyyy-MM-dd", new CultureInfo("en-US"));
    }

    public static string ToShortDateStringBuddhist(this DateTime dateTime)
    {
        return dateTime.ToString("d/MM/yyyy", new CultureInfo("th-TH"));
    }

    public static string ToLongDateStringBuddhist(this DateTime dateTime)
    {
        return dateTime.ToString("dd MMMM yyyy", new CultureInfo("th-TH"));
    }

    public static string ToShortDateTimeStringBuddhist(this DateTime dateTime)
    {
        return dateTime.ToString("d/MM/yyyy HH:mm", new CultureInfo("th-TH"));
    }

    public static string ToLongDateTimeStringBuddhist(this DateTime dateTime)
    {
        return dateTime.ToString("dd MMMM yyyy เวลา HH:mm:ss", new CultureInfo("th-TH"));
    }

    public static int BuddhistYear(this DateTime dateTime)
    {
        return new ThaiBuddhistCalendar().GetYear(dateTime);
    }
    public static int BuddhistBgYear(this DateTime dateTime)
    {
        if (dateTime.Month >= 10)
        {
            return new ThaiBuddhistCalendar().GetYear(dateTime) + 1;
        }
        else
        {
            return new ThaiBuddhistCalendar().GetYear(dateTime) + 0;
        }
    }
}

public static class StringExtensions
{
    private const int LimitInCell = 300;
    public static bool ContainsAnyCaseInsensitive(this string str, params string[] values)
    {
        if (!string.IsNullOrEmpty(str) || values.Length > 0)
        {
            foreach (string value in values)
            {
                if ((str ?? string.Empty).ToLower().Contains(value.ToLower()))
                    return true;
            }
        }

        return false;
    }

    public static bool ContainsAny(this string str, params string[] values)
    {
        if (!string.IsNullOrEmpty(str) || values.Length > 0)
        {
            foreach (string value in values)
            {
                if ((str ?? string.Empty).Contains(value))
                    return true;
            }
        }

        return false;
    }

    public static string[] SplitBySpaceExcludeEmpty(this string str)
    {
        return (from keyword in (str ?? string.Empty).Trim().Split(' ')
                where !string.IsNullOrWhiteSpace(keyword)
                select keyword.Trim().ToLower()).ToArray();
    }

    public static string[] SplitExcludeEmpty(this string str, char splitter)
    {
        return (from keyword in (str ?? string.Empty).Trim().Split(splitter)
                where !string.IsNullOrWhiteSpace(keyword)
                select keyword.Trim().ToLower()).ToArray();
    }

    public static int CountStringOccurrencesCaseInsensitive(this string text, string[] keywords)
    {
        int count = 0;
        foreach (var keyword in keywords)
        {
            // Loop through all instances of the string 'text'.
            int i = 0;
            while ((i = text.IndexOf(keyword, i, StringComparison.OrdinalIgnoreCase)) != -1)
            {
                i += keyword.Length;
                count++;
            }
        }
        return count;
    }

    public static int CountStringOccurrences(this string text, string[] keywords)
    {
        int count = 0;
        foreach (var keyword in keywords)
        {
            // Loop through all instances of the string 'text'.
            int i = 0;
            while ((i = text.IndexOf(keyword, i)) != -1)
            {
                i += keyword.Length;
                count++;
            }
        }
        return count;
    }

    public static string LimitText(this string text, int length = 0)
    {
        if (length == 0) length = LimitInCell;
        return (text ?? string.Empty).Substring(0, Math.Min(length, (text ?? "").Length)) + ((text ?? "").Length > length ? "..." : string.Empty);
    }
     

    public static string TextCell(this string text, int length = 0)
    {
        if (length == 0) length = LimitInCell;
        text = (text ?? string.Empty).Substring(0, Math.Min(length, (text ?? "").Length)) +
                   ((text ?? "").Length > length ? "..." : string.Empty);
        return (text ?? string.Empty).Replace("\n", "<br/>");
    }

    public static string UseHtmlNewLineTag(this string text)
    {
        return (text ?? string.Empty).Replace("\n", "<br/>");
    }
    public static DateTime FromOpmDateDbToDateTime(this string text)
    {
        return FromOpmDateDbToDateTimeNull(text) ?? DateTime.MinValue;
    }
    public static DateTime? FromOpmDateDbToDateTimeNull(this string text)
    {
        text = (text ?? string.Empty);
        DateTime dt;
        if (DateTime.TryParseExact(text, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
        {
            //valid date
            return dt;
        }
        else if (DateTime.TryParseExact(text, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
        {
            //valid date
            return dt;
        }
        else if (DateTime.TryParseExact(text, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
        {
            //valid date
            return dt;
        }
        else
        {
            //invalid date
            return (DateTime?) null;
        }
    }
}